
/***************************************************************************
 *   fecab 
 *                                                                         *
 *   Copyright (C) 2010 by kuerant                                         *
 *                         <kuerant@gmail.com>                             *
 *                                                                         *
 *   This library is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU Library General Public License as       *
 *   published by the Free Software Foundation; either version 2 of the    *
 *   License, or (at your option) any later version.                       *
 *                                                                         *
 *   This library is distributed in the hope that it will be usefull, but  *
 *   WITHOUT ANY WARRANTY; without even the implied warranty of            *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU      *
 *   Library General Public License for more details.                      *
 *                                                                         *
 *   You should have received a copy of the GNU Library General Public     *
 *   License along with this library; if not, write to the Free Software   *
 *   Foundation, Inc., 59 Temple Place - Suite 330, Boston,                *
 *   MA 02111-1307, USA                                                    *
 ***************************************************************************/

#include <cstring>
#include <iostream>

#include "fecab/store.hpp"


#define	CONFIG_FILE	"config.ini"


int	main(void)
{
	try {
		fecab::StoreManager	manager( CONFIG_FILE );

		fecab::Store*	store_p = manager.getStore();

		const char*	TABLE = "table_test";
		u_int32_t	records = 10;

		std::cerr << "put records into table ..." << std::endl;
		for( u_int32_t i=0; i<records; i++) {
			char	key[32];
			u_int32_t	key_size = snprintf(key, sizeof(key) - 1, "key-%09u", i);
			char	data[32];
			u_int32_t	data_size = snprintf(data, sizeof(data) - 1, "data-%09u", i);

			store_p->put( TABLE, key, key_size, data, data_size );
		}
		std::cerr << "put records into table done." << std::endl;

		std::cerr << "get records from table ..." << std::endl;
		for( u_int32_t i=0; i<records; i++) {
			char	key[32];
			u_int32_t	key_size = snprintf(key, sizeof(key) - 1, "key-%09u", i);
			char	data[32];
			u_int32_t	data_size = snprintf(data, sizeof(data) - 1, "data-%09u", i);

			char	buf[32];
			memset(buf, 0, sizeof(buf));

			store_p->get( TABLE, key, key_size, buf, sizeof(buf) );
			if (0 != strcmp(buf, data)) {
				std::cerr << "bad data for key '" << key << "' got '" << buf << "', but want '" << data << "'\n";
			}
		}
		std::cerr << "get records from table done." << std::endl;


		std::cerr << "erase records from table ..." << std::endl;
		for( u_int32_t i=0; i<records; i++) {
			char	key[32];
			u_int32_t	key_size = snprintf(key, sizeof(key) - 1, "key-%09u", i);

			store_p->erase( TABLE, key, key_size );
		}
		std::cerr << "erase records from table done." << std::endl;

		std::cerr << "get records from table after erasing..." << std::endl;
		for( u_int32_t i=0; i<records; i++) {
			char	key[32];
			u_int32_t	key_size = snprintf(key, sizeof(key) - 1, "key-%09u", i);

			char	buf[32];
			memset(buf, 0, sizeof(buf));

			u_int32_t size = store_p->get( TABLE, key, key_size, buf, sizeof(buf) );
			if (size > 0) {
				std::cerr << "bad data for key '" << key << "' got '" << buf << "', but want NULL\n";
			}
		}
		std::cerr << "get records from table after erasing done." << std::endl;

		std::cerr << "erase records from table after ereasing ..." << std::endl;
		for( u_int32_t i=0; i<records; i++) {
			char	key[32];
			u_int32_t	key_size = snprintf(key, sizeof(key) - 1, "key-%09u", i);

			store_p->erase( TABLE, key, key_size );
		}
		std::cerr << "erase records from table after ereasing done." << std::endl;

	}
	catch(std::exception& e) {
		std::cerr << "ERROR : " << e.what() << std::endl;
		return 1;
	}

	std::cerr << "done." << std::endl;
	return	0;
}

